package com.app.terminal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CommandExecutor {
  public String execute(String command) {
    String result = "";
    try {
      Process process = Runtime.getRuntime().exec(command);
      BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
      String line;
      while ((line = reader.readLine()) != null) {
        result += line + "\n";
      }

      // 读取错误流
      BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
      while ((line = errorReader.readLine()) != null) {
        result += line + "\n";
      }

      reader.close();
      errorReader.close();
      process.waitFor();
    } catch (IOException | InterruptedException e) {
      result = "Error executing command: " + e.getMessage();
    }
    return result.trim();
  }
}